package cn.maxwell.cntroller;

import cn.maxwell.block.StoreBlock;
import cn.maxwell.domain.Product;
import cn.maxwell.domain.Store;
import cn.maxwell.openfeign.OPenFeignInterface;
import cn.maxwell.openfeign.StoreProductFeign;
import cn.maxwell.service.IStoreService;
import cn.maxwell.util.JsonResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RefreshScope  //刷新配置
@RestController
@RequestMapping("/store")
public class StoreController {

    @Resource
    private IStoreService storeService;

    @Resource
    private OPenFeignInterface oPenFeignInterface;

    @Value("${server.port}")
    private Integer port;

    @Value("${dev.name}")
    private Object name;

    /**
     * OpenFeign调用product
     *
     * @param id
     * @return
     */
    @RequestMapping("/product/{id}")
    @SentinelResource(value="store-server",fallback = "fallbackUser")
    public Product getById(@PathVariable("id") Long id) {
        Product product = oPenFeignInterface.loadById(id);
        return product;
    }


   /**
     * 单纯的loadbyid
     * @param id
     * @return
     */
    @SentinelResource(value = "store-server", blockHandler = "blockHandlerMethodClass",blockHandlerClass = StoreBlock.class)
    @GetMapping("/loadById/{id}")
    public Store loadById(@PathVariable("id") Long id) {
        System.out.println(name);
        Store store = storeService.loadById(id);
        return store;
    }

/*   *//**
     * 返回jsonresult的loadbyid
     * @param id
     * @return
     *//*
    @GetMapping("/LoadById/{id}")
    public JsonResult jsonLoadById(@PathVariable("id") Long id) {
        try {
            System.out.println(name);
            Store store = storeService.loadById(id);
            return new JsonResult(true,"操作成功！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
    }*/

    /**
     * 添加add
     * @param store
     * @return
     */
    @PostMapping("/add")
    //@SentinelResource(value = "store-server", blockHandler = "blockHandlerMethod",blockHandlerClass = StoreProductFeign.class)
    public JsonResult add(@RequestBody Store store) {
        try {
            storeService.add(store);
            return new JsonResult(true, "操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败！");
        }
    }

    /**
     * 以返回store的方式测试loadbyid的限流
     * @param id
     * @param e
     * @return
     */
    public Store blockHandlerLoadByIdMethod(@PathVariable("id") Long id,BlockException e) {
        e.printStackTrace();
        String str = "我被限流了，暂停服务！";
        System.out.println(str);
        return new Store(-1L,null,null);
    }

   /* *//**
     * 以返回JsonResult的方式测试add方法的限流
     * @param store
     * @param e
     * @return
     *//*
    public JsonResult blockHandlerMethod(@RequestBody Store store, BlockException e) {
        e.printStackTrace();
        return new JsonResult(false,"我被限流了！暂停服务！！！");
    }*/

    /**
     * 以返回JsonResult的方式测试限流：依据方法是loadbyid
     * @param id
     * @param th
     * @return
     */
    public JsonResult fallbackUser(@PathVariable("id") Long id,Throwable th) {
        th.printStackTrace();
        return new JsonResult(false,"我被限流了！暂停服务");
    }
}